import { MES_XHY_ApiHost } from '@/utils/Interceptors'

// 定义系统日志数据类型 - 与实际API返回值字段匹配
export interface SystemLog {
  SysLogsId: number
  StartTime: string
  UserId: number
  UserName: string
  RequestUrl: string
  LogType: string
  ResponseStatus: string
  Duration: number
  RequestParams: string
  ExceptionInfo: string
  UserIP: string
  BrowserType: string
  IsDelete: boolean
}

// 定义API响应类型 - 与实际API返回值字段匹配
export interface SystemLogResponse {
  TotalCount: number
  TotalPage: number
  PageData: SystemLog[]
  code: number
  msg: string
  data: any
}

// 定义查询参数类型
export interface SystemLogQueryParams {
  RequestUrl?: string
  UserIP?: string
  UserName?: string
  ServerIP?: string
  StartTimeFrom?: string
  StartTimeTo?: string
  ResponseStatus?: string
  RoleId?: number
  LogType?: string
  PageIndex?: number
  PageSize?: number
}

// 查询系统日志
export const querySystemLogs = async (params: SystemLogQueryParams) => {
  return await MES_XHY_ApiHost.get<SystemLogResponse>('/api/Login/querySystemLogs', { params })
}

// 导出系统日志
export const exportSystemLogs = async (params: SystemLogQueryParams) => {
  return await MES_XHY_ApiHost.get('/api/Login/exportSystemLogs', { 
    params,
    responseType: 'blob',
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, */*'
    }
  })
}

// 定义批量删除响应类型
export interface BatchDeleteResponse {
  code: number
  msg: string
  data: {
    Success: boolean
    Message: string
    DeletedCount: number
    FailedCount: number
    FailedSysLogsIds: number[]
    OperationTime: string
    DeleteReason: string
  }
}

// 删除系统日志
export const deleteSystemLogs = async (logIds: number[]) => {
  return await MES_XHY_ApiHost.delete('/api/Login/deleteSystemLogs', { 
    data: { logIds } 
  })
}

// 批量删除系统日志
export const batchDeleteSystemLogs = async (sysLogsIds: number[]) => {
  return await MES_XHY_ApiHost.post<BatchDeleteResponse>('/api/Login/batchDeleteSystemLogs', {
    SysLogsIds: sysLogsIds
  })
}
